<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件下载中心 - 文件共享平台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon">
    <style>
        :root {
            --primary: #4361ee;
            --primary-dark: #3a0ca3;
            --light: #f8f9fc;
            --dark: #212529;
            --gray: #6c757d;
        }
        
        body {
            background-color: #f5f7fb;
            color: #333;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }
        
        .header {
            background: linear-gradient(135deg, var(--primary), var(--primary-dark));
            color: white;
            padding: 40px 0;
            margin-bottom: 30px;
        }
        
        .file-card {
            border-radius: 12px;
            border: none;
            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            margin-bottom: 20px;
        }
        
        .file-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 12px 20px rgba(0, 0, 0, 0.15);
        }
        
        .file-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, var(--primary), var(--primary-dark));
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 20px;
            color: white;
            font-size: 28px;
        }
        
        .storage-card {
            background-color: white;
            border-radius: 12px;
            padding: 25px;
            text-align: center;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
            margin-bottom: 30px;
        }
        
        .storage-number {
            font-size: 2.5rem;
            font-weight: 700;
            color: var(--primary);
            margin: 10px 0;
        }
        
        .storage-label {
            color: var(--gray);
            font-size: 1rem;
        }
        
        .progress {
            height: 10px;
            margin: 15px 0;
        }
        
        .download-btn {
            background: var(--primary);
            border: none;
            border-radius: 30px;
            padding: 8px 20px;
            font-weight: 500;
            transition: all 0.3s;
        }
        
        .download-btn:hover {
            background: var(--primary-dark);
            transform: translateY(-2px);
        }
        
        .file-info {
            display: flex;
            justify-content: space-between;
            font-size: 0.9rem;
            color: var(--gray);
            margin-top: 10px;
        }
        
        .empty-state {
            text-align: center;
            padding: 50px 0;
        }
        
        .empty-icon {
            font-size: 4rem;
            color: #dee2e6;
            margin-bottom: 20px;
        }
        
        .file-type-icon {
            font-size: 24px;
            margin-right: 15px;
            color: var(--primary);
        }

        /* 深色模式变量 */
        :root {
          --primary-light: #4361ee;
          --primary-dark: #3a0ca3;
          --light: #f8f9fc;
          --dark: #212529;
          --gray: #6c757d;
          --text-color: #333;
          --bg-color: #f5f7fb;
          --card-bg: #ffffff;
          --header-bg: linear-gradient(135deg, var(--primary-light), var(--primary-dark));
          --border-color: #dee2e6;
          --input-bg: #ffffff;
          --text-muted: #6c757d;
        }
        
        [data-theme="dark"] {
          --primary-light: #5a76ff;
          --primary-dark: #4a5ad4;
          --light: #1a1c23;
          --dark: #f8f9fa;
          --gray: #a0a8b0;
          --text-color: #e0e0e0;
          --bg-color: #121418;
          --card-bg: #1e2229;
          --header-bg: linear-gradient(135deg, #2a3a8d, #1a1a5e);
          --border-color: #2d333b;
          --input-bg: #252b33;
          --text-muted: #a0a8b0;
        }
        
        /* 通用深色模式覆盖 */
        [data-theme="dark"] body {
          background-color: var(--bg-color);
          color: var(--text-color);
        }
        
        [data-theme="dark"] .github-card {
          background-color: var(--card-bg);
          border-color: var(--border-color);
        }
        
        [data-theme="dark"] .github-header {
          background: var(--header-bg);
        }
        
        [data-theme="dark"] .text-muted {
          color: var(--text-muted) !important;
        }
        
        [data-theme="dark"] .form-control {
          background-color: var(--input-bg);
          color: var(--text-color);
          border-color: var(--border-color);
        }
        
        .theme-switch-container {
          position: fixed;
          top: 15px;
          right: 15px;
          z-index: 1000;
        }
        
        #theme-toggle {
          background-color: rgba(255, 255, 255, 0.2);
          color: white;
          border: none;
          box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }
        
        [data-theme="dark"] #theme-toggle {
          background-color: rgba(0, 0, 0, 0.2);
        }
    </style>
</head>
<body>
    <header class="header">
        <div class="container text-center">
            <h1><i class="bi bi-download"></i> 文件下载中心</h1>
            <h2> (访客中心)</h2>
            <p class="lead">浏览并下载共享文件</p>
        </div>
    </header>
    
    <div class="container">
        <div class="row">
            <div class="col-lg-4">
                <div class="storage-card">
                    <i class="bi bi-hdd display-4 text-primary mb-3"></i>
                    <div class="storage-label">已用空间</div>
                    <div class="storage-number" id="used-space">0B</div>
                    
                    <div class="storage-label">可用空间</div>
                    <div class="storage-number" id="free-space">0B</div>

                    <div class="progress">
                        <div class="progress-bar" id="usage-bar" role="progressbar"></div>
                    </div>
                    
                    <div class="mt-3">
                        <span id="usage-percent">0%</span> 使用率
                    </div>
                </div>
                
                <div class="card file-card">
                    <div class="card-body text-center">
                        <a href="/index" class="btn btn-outline-light btn-block">
                            <i class="bi bi-info-circle display-4 text-primary mb-3"></i>
                        </a>
                        <h5>使用说明</h5>
                        <p class="text-muted">
                            点击文件卡片上的下载按钮即可下载文件
                        </p>
                        <p class="text-muted">
                            文件按修改时间排序，最新文件显示在最前面
                        </p>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-8">
                <div class="card file-card">
                    <div class="card-header bg-white">
                        <h5 class="mb-0"><i class="bi bi-files"></i> 文件列表</h5>
                    </div>
                    <div class="card-body">
                        <div id="files-container">
                            <div class="text-center py-5">
                                <div class="spinner-border text-primary" role="status">
                                    <span class="visually-hidden">加载中...</span>
                                </div>
                                <p class="mt-2">正在加载文件列表...</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <footer class="text-center py-4 mt-5 text-muted">
        <p>© 2025 508364.All rights reserved.</p>
    </footer>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const filesContainer = document.getElementById('files-container');
            const usedSpaceEl = document.getElementById('used-space');
            const freeSpaceEl = document.getElementById('free-space');
            const usagePercentEl = document.getElementById('usage-percent');
            const usageBar = document.getElementById('usage-bar');
            
            // 辅助函数：获取文件类型
            function getFileType(filename) {
                if (!filename) return 'unknown';
                
                const extension = filename.split('.').pop().toLowerCase();
                const types = {
                    // 图片类型
                    jpg: 'image', jpeg: 'image', png: 'image', gif: 'image', bmp: 'image', 
                    webp: 'image', svg: 'image', tiff: 'image',
                    
                    // 文档类型
        pdf: 'document', doc: 'document', docx: 'document', xls: 'document', 
        xlsx: 'document', ppt: 'document', pptx: 'document', txt: 'document',
        rtf: 'document', odt: 'document', ods: 'document', odp: 'document',
        
        // 压缩文件
        zip: 'archive', rar: 'archive', '7z': 'archive', tar: 'archive', 
        gz: 'archive', bz2: 'archive',
        
        // 音频文件
        mp3: 'audio', wav: 'audio', ogg: 'audio', flac: 'audio', aac: 'audio',
        
        // 视频文件
        mp4: 'video', mov: 'video', avi: 'video', mkv: 'video', wmv: 'video',
        flv: 'video', webm: 'video',
        
        // 代码文件
        js: 'code', html: 'code', css: 'code', py: 'code', java: 'code', 
        cpp: 'code', c: 'code', cs: 'code', php: 'code', json: 'code', 
        xml: 'code', sql: 'code',
        
        // 其他
        exe: 'filetype', dll: 'executable', apk: 'android', 
        iso: 'disk_image', dmg: 'disk_image', aktp: 'aktp'
    };
                
                return types[extension] || 'unknown';
            }
            
            // 辅助函数：获取文件图标
            function getFileIcon(fileType) {
                const icons = {
                    image: 'bi-file-image',
        document: 'bi-file-earmark-text',
        archive: 'bi-file-earmark-zip',
        audio: 'bi-file-earmark-music',
        video: 'bi-file-earmark-play',
        code: 'bi-file-earmark-code',
        filetype: 'bi bi-filetype-exe',
        android: 'bi bi-android2',
        executable: 'bi-file-earmark-binary',
        disk_image: 'bi-file-earmark-break',
        aktp: 'bi-filetype-json',
        unknown: 'bi-file-earmark'
    };
                
                return icons[fileType] || 'bi-file-earmark';
            }
            
            // 辅助函数：格式化字节大小
            function formatBytes(bytes, decimals = 2) {
                if (bytes === 0 || isNaN(bytes)) return '0 Bytes';
                
                const k = 1024;
                const dm = decimals < 0 ? 0 : decimals;
                const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
                
                const i = Math.floor(Math.log(bytes) / Math.log(k));
                
                return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
            }
            
            // 加载文件列表和存储空间信息
            function loadData() {
                // 获取文件列表
                fetch('/api/files')
                    .then(response => response.json())
                    .then(data => {
                        renderFiles(data.files);
                    })
                    .catch(error => {
                        console.error('加载文件列表失败:', error);
                        filesContainer.innerHTML = `
                            <div class="alert alert-danger">
                                <i class="bi bi-exclamation-triangle"></i> 加载文件列表失败
                            </div>
                        `;
                    });
                
                // 获取存储空间信息
                fetch('/api/sysinfo')
                    .then(response => response.json())
                    .then(data => {
                        if (data.disk) {
                            updateStorageInfo(data.disk);
                        }
                    })
                    .catch(error => {
                        console.error('加载存储信息失败:', error);
                    });
            }
            
            // 更新存储空间信息
            function updateStorageInfo(diskInfo) {
                usedSpaceEl.textContent = formatBytes(diskInfo.upload_used);
                freeSpaceEl.textContent = formatBytes(diskInfo.available);
                
                const percent = Math.min(100, Math.round(
                    (diskInfo.upload_used / diskInfo.upload_total) * 100
                ));
                
                usagePercentEl.textContent = `${percent}%`;
                usageBar.style.width = `${percent}%`;
                
                // 根据使用率设置颜色
                if (percent > 90) {
                    usageBar.classList.add('bg-danger');
                } else if (percent > 70) {
                    usageBar.classList.add('bg-warning');
                } else {
                    usageBar.classList.add('bg-success');
                }
            }
            
            // 渲染文件列表
            function renderFiles(files) {
                if (!files || files.length === 0) {
                    filesContainer.innerHTML = `
                        <div class="empty-state">
                            <i class="bi bi-folder-x empty-icon"></i>
                            <h5 class="text-muted">暂无文件</h5>
                            <p>当前没有可下载的文件</p>
                        </div>
                    `;
                    return;
                }
                
                let html = '';
                
                // 按修改时间排序（最新在前）
                files.sort((a, b) => b.modified - a.modified);
                
                files.forEach(file => {
                    const modifiedDate = new Date(file.modified * 1000).toLocaleDateString();
                    const fileType = getFileType(file.filename);
                    const iconClass = getFileIcon(fileType);
                    
                    html += `
                        <div class="card file-card mb-3">
                            <div class="card-body">
                                <div class="d-flex align-items-center">
                                    <div class="file-type-icon">
                                        <i class="bi ${iconClass}"></i>
                                    </div>
                                    
                                    <div class="flex-grow-1">
                                        <h5 class="card-title mb-1">${file.name}</h5>
                                        <div>
                                            <a href="/file_detail?file=${encodeURIComponent(file.name)}">查看详情</a>
                                        </div>
                                        <div class="file-info">
                                            <span>${formatBytes(file.size)}</span>
                                            <span>上传于: ${modifiedDate}</span>
                                        </div>
                                    </div>
                                    <a href="/download/${encodeURIComponent(file.filename)}" class="btn btn-sm btn-outline-primary">
                                        <i class="bi bi-download me-1"></i> 下载
                                    </a>
                                </div>
                            </div>
                        </div>
                    `;
                });
                
                filesContainer.innerHTML = html;
            }
            
            // 初始加载数据
            loadData();
            
            // 每1分钟刷新一次数据
            setInterval(loadData, 1 * 60 * 1000);
        });
    </script>
    
    <!-- 左下角深色模式切换控件 -->
    <div class="bottom-left-controls">
        <button id="theme-toggle" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-moon"></i> 深色模式
        </button>
    </div>
    
    <!-- 深色模式切换脚本 -->
    <script>
        // 深色模式切换功能
        document.addEventListener('DOMContentLoaded', function() {
            const themeToggle = document.getElementById('theme-toggle');
            
            // 检查本地存储或系统偏好
            const savedTheme = localStorage.getItem('theme') || 
                            (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
            
            if (savedTheme === 'dark') {
                document.documentElement.setAttribute('data-theme', 'dark');
                themeToggle.innerHTML = '<i class="bi bi-sun"></i> 浅色模式';
                document.body.style.backgroundColor = '#1a1a2e';
                document.body.style.color = '#e0e0e0';
            }
            
            // 切换主题
            themeToggle.addEventListener('click', function() {
                const currentTheme = document.documentElement.getAttribute('data-theme');
                if (currentTheme === 'dark') {
                    document.documentElement.removeAttribute('data-theme');
                    localStorage.setItem('theme', 'light');
                    themeToggle.innerHTML = '<i class="bi bi-moon"></i> 深色模式';
                    document.body.style.backgroundColor = '#f5f7fb';
                    document.body.style.color = '#333';
                } else {
                    document.documentElement.setAttribute('data-theme', 'dark');
                    localStorage.setItem('theme', 'dark');
                    themeToggle.innerHTML = '<i class="bi bi-sun"></i> 浅色模式';
                    document.body.style.backgroundColor = '#1a1a2e';
                    document.body.style.color = '#e0e0e0';
                }
            });
        });
    </script>
    
    <!-- 左下角深色模式切换控件样式 -->
    <style>
        .bottom-left-controls {
            position: fixed;
            bottom: 15px;
            left: 15px;
            z-index: 1000;
        }
        
        .bottom-left-controls button {
            padding: 8px 12px;
            border-radius: 20px;
            transition: all 0.3s ease;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
        }
        
        .bottom-left-controls button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
        }
        
        /* 深色模式下的按钮样式 */
        [data-theme="dark"] .bottom-left-controls button {
            background-color: #252b33;
            color: #e0e0e0;
            border-color: #495057;
        }
        
        [data-theme="dark"] .bottom-left-controls button:hover {
            background-color: #343a40;
        }
    </style>
</body>
</html>
